package stock.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import stock.pojo.Result;
import stock.pojo.SecKillGoods;
import stock.service.StockService;

@RestController
@RequestMapping("/StockSeaContral")
public class StockSeaContral {

    @Autowired
    StockService stockService;

    @PostMapping("/deduct")
    public Result<Object> deduct(@RequestBody SecKillGoods secKillGoods) {
        if (secKillGoods.getSum() == 0) {
            return new Result<>();
        }
        String deduct="操作失败";
        for (int i = 0; i < 3; i++) {//CAS机制
             deduct= stockService.deduct(secKillGoods);
            if("操作成功".equals(deduct)){ break;}
        }

        return new Result<>(deduct, 200,null );
    }


}
